<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上级菜单"
        >
          <a-select placeholder="上级菜单" :disabled="isDisabled"
                    v-decorator="[ 'pid', {rules: []} ]">
            <a-select-option value="">请选择上级菜单</a-select-option>
            <a-select-option v-for="item in parentMenus" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="菜单名称"
        >
          <a-input
            placeholder="菜单名称"
            v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="权限唯一键"
        >
          <a-input
            placeholder="权限唯一键"
            v-decorator="[ 'keySign', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="菜单图标"
        >
          <a-input
            placeholder="菜单图标"
            v-decorator="[ 'icon', {rules: []}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="component"
        >
          <a-input
            placeholder="component"
            v-decorator="[ 'component', {rules: []}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序"
        >
          <a-input-number
            placeholder="排序"
            v-decorator="[ 'sort', {rules: [], initialValue: 1000}]"
            :min="1000"
            :max="99999"
          ></a-input-number>
        </a-form-item>

        <div v-show="form.getFieldValue('pid') != undefined && form.getFieldValue('pid') != ''">
          <a-divider/>
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="赋予权限"
          >
            <a-select mode="multiple" placeholder="赋予权限"
                      v-decorator="[ 'actions', {rules: []} ]">
              <a-select-option value="add">新增</a-select-option>
              <a-select-option value="query">查询</a-select-option>
              <a-select-option value="update">修改</a-select-option>
              <a-select-option value="delete">删除</a-select-option>
              <a-select-option value="import">导入</a-select-option>
              <a-select-option value="export">导出</a-select-option>
              <a-select-option value="other">特殊</a-select-option>
            </a-select>
          </a-form-item>
        </div>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save, selectByNull } from '@/api/system/menu'
  import pick from 'lodash.pick'

  export default {
    name:'Edit',
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,
        parentMenus: []
      }
    },
    //--############  data() ###############-->

    created() {
      selectByNull().then(res => {
        this.parentMenus = res.result
      })
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
        selectByNull().then(res => {
          this.parentMenus = res.result
        })
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'pid', 'name', 'keySign', 'icon', 'component', 'sort', 'actions'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          console.log(values)
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
